﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using DBUtility;


namespace CommonFunc
{
    public class CommonFunction
    {

        #region 封装字段
        private Label lB_Thumb;

        /// <summary>
        /// 存储缩略图名称的Label
        /// </summary>
        public Label LB_Thumb
        {
            get { return lB_Thumb; }
            set { lB_Thumb = value; }
        }
        #endregion


        #region "'"字符过滤函数
        /// <summary>
        /// "'"字符过滤函数
        /// </summary>
        /// <param name="SQLstr">需要过滤的字符串</param>
        /// <returns></returns>
        public static string ReplaceStr(string SQLstr)
        {
            if (SQLstr == null || SQLstr == "")
            {
                return "";
            }
            else
            {
                SQLstr = SQLstr.Replace("'", "");
                return SQLstr;
            }
        }
        #endregion


        #region 字符串过滤函数
        /// <summary>
        /// URL特殊字符过滤函数
        /// </summary>
        /// <param name="SQLstr">需要过滤的字符串</param>
        /// <returns></returns>
        public static string ReplaceStr2(string SQLstr)
        {
            if (SQLstr == null || SQLstr == "")
            {
                return "";
            }
            else
            {
                SQLstr = SQLstr.Replace("\"", "");
                SQLstr = SQLstr.Replace("#", "");
                SQLstr = SQLstr.Replace("(", "");
                SQLstr = SQLstr.Replace(")", "");
                SQLstr = SQLstr.Replace("/", "");
                SQLstr = SQLstr.Replace(":", "");
                SQLstr = SQLstr.Replace(";", "");
                SQLstr = SQLstr.Replace("@", "");
                SQLstr = SQLstr.Replace("\\", "");
                SQLstr = SQLstr.Replace("|", "");
                SQLstr = SQLstr.Replace("'", "");
                SQLstr = SQLstr.Replace("&", "");
                SQLstr = SQLstr.Replace("%20", "");
                SQLstr = SQLstr.Replace("<", "");
                SQLstr = SQLstr.Replace(">", "");
                SQLstr = SQLstr.Replace("%", "");
                SQLstr = SQLstr.Replace("+", "");
                SQLstr = SQLstr.Replace("-", "");
                SQLstr = SQLstr.Replace("=", "");
                SQLstr = SQLstr.Replace(",", "");
                return SQLstr;
            }

        }
        #endregion


        #region TextBox字符转换函数
        /// <summary>
        /// 将TextBox的字符格式转换为Html字符格式
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <returns></returns>
        public static string CharConvert(string str)
        {
            str = str.Replace(">", "&gt;");
            str = str.Replace("<", "&lt;");
            char ch;
            ch = (char)32;
            str = str.Replace(ch.ToString(), "&nbsp;");
            ch = (char)34;
            str = str.Replace(ch.ToString(), "&quot;");
            ch = (char)39;
            str = str.Replace(ch.ToString(), "'");
            ch = (char)13;
            str = str.Replace(ch.ToString(), "");
            ch = (char)10;
            str = str.Replace(ch.ToString(), "<br />");
            return str;
        }
        #endregion


        #region Textbox字符反转换函数
        /// <summary>
        /// 将Html的格式转换为TextBox的字符格式
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <returns></returns>
        public static string CharReConvert(string str)
        {
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            char ch;
            ch = (char)32;
            str = str.Replace("&nbsp;", ch.ToString());
            ch = (char)34;
            str = str.Replace("&quot;", ch.ToString());
            ch = (char)39;
            str = str.Replace("'", ch.ToString());
            ch = (char)10;
            str = str.Replace("<br />", ch.ToString());
            return str;
        }
        #endregion


        #region 文件删除函数
        /// <summary>
        /// 文件删除函数
        /// </summary>
        /// <param name="_FileURL">文件的绝对路径</param>
        /// <returns></returns>
        public static bool DelFile(string _FileURL)
        {
            if (File.Exists(_FileURL))
            {
                try
                {
                    File.Delete(_FileURL);
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }
        #endregion


        #region 随机文件名生成函数
        /// <summary>
        /// 由系统当前LongDateTime时间后面追加7位随即数字组成
        /// </summary>
        /// <returns></returns>
        public static string DateTimeRandomFileName()
        {
            string DT = DateTime.Now.ToString();
            DT = DT.Replace("-", "").Replace(" ", "").Replace(":", "");
            Random Ro = new Random();
            string str = DT + Ro.Next(1000000, 9999999);
            return str;
        }
        #endregion


        #region 获取15位随机数字
        /// <summary>
        /// 获取15位随机数字
        /// </summary>
        /// <returns></returns>
        public static string Get15RandomCode()
        {
            Random Ro = new Random();
            string str = Ro.Next(10000, 99999).ToString() + Ro.Next(10000, 99999).ToString() + Ro.Next(10000, 99999).ToString();
            return str;
            
        }
        #endregion


        #region 判断文本字段是否重复函数
        /// <summary>
        /// 判断数据库中某列是否含有该文本信息
        /// </summary>
        /// <param name="_Field">文本信息</param>
        /// <param name="_TableField">数据表名</param>
        /// <param name="_ColumnField">字段名</param>
        /// <returns></returns>
        public static bool CheckFieldIfRepeat(string _Field, string _TableField, string _ColumnField)
        {
            string SQLstr_CheckNameIfRepeat = "SELECT COUNT(*) FROM [" + _TableField + "] WHERE [" + _ColumnField + "]='" + _Field + "'";
            int num = AccHelper.GetIntExecute(SQLstr_CheckNameIfRepeat);
            if (num > 0)
            {
                //如果此用户名已存在则返回false
                return false;
            }
            else
            {
                //如果此用户名不存在则返回true
                return true;
            }
        }
        #endregion


        #region MD5转换函数
        /// <summary>
        /// MD5转换函数
        /// </summary>
        /// <param name="_str"></param>
        /// <returns></returns>
        public static string GetMD5string(string _str)
        {
            string str = FormsAuthentication.HashPasswordForStoringInConfigFile(_str, "MD5");
            return str;
        }
        #endregion


        #region 字符串截断函数
        /// <summary>
        /// 字符串截断函数
        /// </summary>
        /// <param name="_Str">需要截取的字符串</param>
        /// <param name="num">需要截取的长度</param>
        /// <param name="_DotStr">截取后补上的文本，如“……”</param>
        /// <returns></returns>
        public static string SubStr(string _Str, int num, string _DotStr)
        {
            if (_Str.Length > num)
            {
                _Str = _Str.Substring(0, num) + _DotStr;
                return _Str;
            }
            else
            {
                return _Str;
            }
        }
        #endregion


        #region 页面跳转函数
        /// <summary>
        /// 页面跳转函数 使用此函数的页面请创建一个 id="aa" 的div
        /// </summary>
        /// <param name="_PageName">跳转的页面名称</param>
        /// <param name="_Page">当前页面Page对象</param>
        public static void JumpAnotherPage(string _PageName, Page _Page)
        {

            // this.mytext.InnerHtml="三秒后页面跳转<div id=\"aa\"></div>";//也可以在页面固定一个DIV区域来显示该信息
            //Response.Write("三秒后页面跳转<div id=\"aa\"></div>");//先生成一个用来显示时间倒计时的DIV

            StringBuilder SB = new StringBuilder();//注意添加using引用 System.Text
            SB.Append("<script langage=\"javascript\">"); //用\"转义'

            SB.Append("var i=4;");

            SB.Append("function out()");

            SB.Append("{");

            SB.Append("if(i>0){ ");

            SB.Append("i--;}");

            SB.Append("else {");

            SB.Append("location.href=\"");

            SB.Append(_PageName);

            SB.Append("\";}");

            SB.Append("document.getElementById(\"aa\").innerHTML=i; }");

            SB.Append("setInterval(\"out()\",1000); "); //每隔一秒发生

            SB.Append("</script>");

            _Page.Controls.Add(new LiteralControl(SB.ToString()));

        }
        #endregion


        #region 正则过滤函数
        /// <summary>
        /// 正则过滤函数
        /// </summary>
        /// <param name="_str">需要过滤的字符串</param>
        /// <returns></returns>
        public static string RegexReplace(string _str)
        {
            return System.Text.RegularExpressions.Regex.Replace(_str, "<[^>]+>", "");
        }
        #endregion
    }
}
